<?php
/* Модель для таблицы запросов */

class multitender_model_list_queries extends multitender_model {

    public $id;                 /* Иидентификатор */
    public $crc;                /* CRC строки serialize */
    public $arr_search;         /* Массив запроса */
    public $ser_search;         /* Строка запроса засервайсена */
    public $total;              /* Количество результатов */
    public $region;             /* Регион */
    public $okrug;		/* Округ */
    public $main; 		/* Строка запроса */
    public $arr_query_search;   /* Строка запроса с морфологией */
    public $ser_query_search;   /* Строка запроса засервайсена с морфологией */
    public $crc_query;          /* СRC по $sqer_query_search */

    function  __construct() {
        parent::__construct();
        $this->db      = & $this->conf['dbs']['person'];
    }

    public function get_query_id(&$obj, $total) {

        if (is_null($this->db)) {
            return false;
        }

        $this->arr_search = $obj->search;
        $this->ser_search = serialize($this->arr_search);
        $this->crc = abs(crc_p($this->ser_search));
        //sscanf(crc_p($this->ser_search), '%u', $this->crc);
        $this->arr_query_search = $obj->search_query;
        $this->ser_query_search = serialize($this->arr_query_search);
        sscanf(crc_p($this->ser_query_search), '%u', $this->crc_query);

        $this->total  = $total;
        $this->region = isset($this->arr_search['reg']) ? key($this->arr_search['reg']) : 0;
        $this->okrug  = isset($this->arr_search['okr']) ? key($this->arr_search['okr']) : 0;
        $this->main   = isset($this->arr_search['main']) ? $this->arr_search['main'] : null;

        $sql="SELECT id FROM search WHERE crc=? AND search=?";
        $arr=array($this->crc,$this->ser_search);                  
        $ans=$this->db->GetOne($sql,$arr);        
        if ($ans) {
            $sql="UPDATE search SET total=?, total_update=? WHERE id=?";
            $this->db->Execute($sql, array($total, $this->db->BindTimeStamp(time()), $ans));
            $this->id=$ans;
            return $ans;
        } else {
            $sql="INSERT INTO search(crc, search, total, total_update, region, okrug, main, morf_crc, morf_search) VALUES(?,?,?,?,?,?,?,?,?)";

            $r=0;
            $colision=false;

            $now_date = $this->db->BindTimeStamp(time());

            $arr=array($this->crc, $this->ser_search, $this->total, $now_date, $this->region, $this->okrug, $this->main, $this->crc_query, $this->ser_query_search);

            $ans=$this->db->Execute($sql, $arr);
            if ($ans) {
                $this->id=$this->db->Insert_ID();
                return $this->id;
            } else {
                return false;
            }
        }
    }


    public function get($id) {
        if (is_null($this->db)) {
            return false;
        }
        $sql="SELECT search FROM search WHERE id=".$id;
        return $this->db->GetOne($sql);
    }

    public function getList($ids) {
        if (!$ids) {
            return false;
        }
        return $this->db->GetAll("SELECT * FROM search WHERE id IN (".implode($ids,',').") ORDER BY FIELD(id,".implode($ids,',').");");
    }

}
